package dialog;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.TextView;

import com.example.zx.theline.jclr.JclrActivity;
import com.example.zx.theline.R;

import org.apache.http.Header;

import java.util.ArrayList;
import java.util.List;

import GsonBean.SuccessBean;
import GsonBean.post.QdzgrPost;
import Utils.RefreshManager;
import app.MyApplication;
import base.BaseDialog;
import greendao.UserDao;
import greendao.entity.User;
import network.NetWorkApi;
import network.ObjectHttpResponseHandler;
import Utils.MyToast;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by zx on 2017/4/12.
 */
//指定整改人
public class ZdzgrDialog extends BaseDialog {

    AutoCompleteTextView mAutoCompleteTextView;
    //选中整改人
    TextView xzzgr;

    //监督检查计划表主键
    String jdjhjcbId;

    //日计划号
    String jdjhbBh;

    //责任单位名称
    String zrDwMc;
    //本单位人员信息list
    List<User> mUserList;

    TextView queding;
    TextView quxiao;

    //确定整改人PostBean
    QdzgrPost mQdzgrPost;

    //AutoCompleteTextView选中position
    int mPosition;

    Activity baseActivity;

    public ZdzgrDialog(Context context, String jdjhjcbId, String jdjhbBh, String zrDwMc, Activity baseActivity) {
        super(context);
        this.jdjhjcbId = jdjhjcbId;
        this.jdjhbBh = jdjhbBh;
        this.zrDwMc = zrDwMc;
        this.baseActivity = baseActivity;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_zdzzrdialog);
        InitView();
        InitData();
        InitListen();
    }

    @Override
    protected void InitView() {
        xzzgr = (TextView) findViewById(R.id.xzzgr);
        queding = (TextView) findViewById(R.id.queding);
        quxiao = (TextView) findViewById(R.id.quxiao);
        mAutoCompleteTextView = (AutoCompleteTextView) findViewById(R.id.spinner);
    }

    @Override
    protected void InitData() {
        setSpinnerData();
        mQdzgrPost = new QdzgrPost();
    }

    @Override
    protected void InitListen() {

        mAutoCompleteTextView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                mPosition = position;
                if(((User) mAutoCompleteTextView.getAdapter().getItem(position)).getDownUnitName()!=null){
                    xzzgr.setText(((User) mAutoCompleteTextView.getAdapter().getItem(position)).getDownUnitName() + "||" + ((User) mAutoCompleteTextView.getAdapter().getItem(position)).getLjyyxWorkerName());
                } else {
                    xzzgr.setText(((User) mAutoCompleteTextView.getAdapter().getItem(position)).getLjyyxDwxxName()+ "||" + ((User) mAutoCompleteTextView.getAdapter().getItem(position)).getLjyyxWorkerName());

                }
            }
        });
        queding.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (xzzgr.getText().toString().equals("")) {
                    MyToast.showToast("请选择整改人");
                } else {
                    MyApplication.ShowWaitDialog(getContext());
                    mQdzgrPost.setJdjhjcbId(jdjhjcbId);
                    mQdzgrPost.setZgrId(((User) mAutoCompleteTextView.getAdapter().getItem(mPosition)).getLjyyxLoginName());
                    mQdzgrPost.setZgrName(((User) mAutoCompleteTextView.getAdapter().getItem(mPosition)).getLjyyxWorkerName());
                    mQdzgrPost.setZrDwMc(zrDwMc);
                    mQdzgrPost.setXgrId(MyApplication.getmLoginUser().getUserDisplayName());

                    //审核单位流转信息list
                    List<QdzgrPost.LjyyxRjdjhjcbShdw> jcbShdwList = new ArrayList<QdzgrPost.LjyyxRjdjhjcbShdw>();
                    QdzgrPost.LjyyxRjdjhjcbShdw shdw = new QdzgrPost.LjyyxRjdjhjcbShdw();
                    shdw.setJdjhbBh(jdjhbBh);
                    shdw.setDwId(MyApplication.getmLoginUser().getUserUnitId());
                    shdw.setDwmc(MyApplication.getmLoginUser().getUserUnitName());
                    shdw.setShrlogin(MyApplication.getmLoginUser().getUserDisplayName());
                    shdw.setShrname(MyApplication.getmLoginUser().getTrueName());
                    shdw.setShrphone(MyApplication.getmLoginUser().getUserMobilephone());
                    jcbShdwList.add(shdw);
                    mQdzgrPost.setJcbShdwList(jcbShdwList);

                    NetWorkApi.Qdzgr(mQdzgrPost, new Callback<SuccessBean>() {
                                @Override
                                public void onResponse(Call<SuccessBean> call, Response<SuccessBean> response) {
                                    try {
                                        SuccessBean mBean = response.body();
                                        if (mBean.getMsg().equals("success") && mBean.getCount() != 0) {
                                            MyToast.showToast("指定整改人成功");
                                            RefreshManager.Jdjc.RefreshJdjclrxx();
                                            RefreshManager.Jdjc.RefreshJdjcdcl();
                                            RefreshManager.Jdjc.RefreshJdjcycl();
                                            ZdzgrDialog.this.dismiss();
                                            getContext().startActivity(new Intent(getContext(), JclrActivity.class));
                                        }
                                    } catch (Exception e) {
                                        MyToast.showFail();
                                    }
                                    MyApplication.HideWaitDialog();
                                }

                                @Override
                                public void onFailure(Call<SuccessBean> call, Throwable t) {
                                    MyToast.showFail();
                                }
                            }
                    );
                }
            }
        });

        quxiao.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ZdzgrDialog.this.dismiss();
            }
        });
    }


    public void setSpinnerData() {
        //根据登录用户单位ID获取该单位所有用户信息
        mUserList = MyApplication.getDaoSession().getUserDao().loadAll();
        mAutoCompleteTextView.setAdapter(new ArrayAdapter<User>(getContext(), R.layout.support_simple_spinner_dropdown_item, mUserList));
    }


}
